package com.winit.tree.repository;

import com.winit.tree.model.TreeNode;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaUpdate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

@Repository
public class TreeRepositoryExt {
    @PersistenceContext
    private EntityManager em;

    @Transactional
    public void updateByPropertyNotNull(TreeNode treeNode){
        CriteriaBuilder cb=em.getCriteriaBuilder();
        CriteriaUpdate<TreeNode> op=cb.createCriteriaUpdate(TreeNode.class);
        Root<TreeNode> root=op.from(TreeNode.class);
        if (null != treeNode.getName()) {
            op.set("name", treeNode.getName());
        }
        if (null != treeNode.getIsDel()) {
            op.set("isDel", treeNode.getIsDel());
        }
//        if (null != treeNode.getLevel()) {
//            op.set("level", treeNode.getLevel());
//        }
        if (null != treeNode.getParentId()) {
            op.set("parentId", treeNode.getParentId());
        }
        //树节点类型不能修改
//        if (null != treeNode.getType()) {
//            op.set("type", treeNode.getType());
//        }
        op.where(cb.equal(root.get("id"), treeNode.getId()));
        em.createQuery(op).executeUpdate();
    }


}
